﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhongMachTu.OModel;
using PhongMachTu.OConnection;

namespace PhongMachTu.OForm
{
    public partial class FormMedBill : Form
    {
        private ArrayList patients;
        private SettingFee medicalFee;
        private int curPaID;
        private int viID;

        public FormMedBill()
        {
            InitializeComponent();
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            if (OSession.isLoggedIn())
            {
                double a;
                if (double.TryParse(txtTotal.Text, out a))
                {
                    if (DataManipulate.pay(viID, double.Parse(txtTotal.Text)))
                    {
                        MessageBox.Show("Thanh toán thành công.");
                        patients = DataManipulate.getPaForPay();
                        loadData();
                        curPaID = 0;
                        viID = 0;
                        getDataForLV();
                        clearTXT();
                    }
                    else
                    {
                        MessageBox.Show("Thanh toán thất bại. Mời thanh toán lại");
                    }
                }
            }
            else
            {
                FormManager.loginForm.Show();
                FormManager.medBillForm.Hide();
                MessageBox.Show("Quá thời gian chờ cho phép, vui lòng đăng nhập lại");
            }
        }

        private void cbFullname_SelectedIndexChanged(object sender, EventArgs e)
        {
            int number;
            if (cbFullname.SelectedIndex >= 0)
            {
                if (Int32.TryParse(cbFullname.SelectedValue.ToString(), out number))
                {
                    curPaID = (int)cbFullname.SelectedValue;
                    Patient pa = (Patient)patients[cbFullname.SelectedIndex];
                    viID = pa.ViID;
                    txtPatientID.Text = curPaID.ToString();
                    txtViDate.Text = pa.VisitDate.ToString();
                    getDataForLV();
                    txtMedicalFee.Text = medicalFee._value.ToString();
                    txtTotal.Text = (double.Parse(txtMedicalFee.Text) + double.Parse(txtMedicineFee.Text)).ToString();
                }
                else
                {
                    curPaID = 0;
                }
            }
        }

        public void loadData()
        {
            patients = DataManipulate.getPaForPay();
            medicalFee = DataManipulate.getSettingFee();

            cbFullname.DataSource = patients;
            cbFullname.DisplayMember = "Fullname";
            cbFullname.ValueMember = "PatienId";
            cbFullname.SelectedIndex = -1;

        }

        public void getDataForLV()
        {
            if (curPaID > 0)
            {
                lvMed.Items.Clear();
                ArrayList viMeds = DataManipulate.getViMed(DataManipulate.getViID(curPaID));
                double totalMedPrice = 0;
                if (viMeds != null)
                {
                    for (int i = 0; i < viMeds.Count; i++)
                    {
                        int stt = i + 1;
                        VisitMedicine viMed = (VisitMedicine)viMeds[i];
                        string medicine = viMed.MedInfo.Name;
                        string unit = viMed.MedInfo.Unit.Name;
                        int quantity = viMed.Quantity;
                        double price = viMed.MedInfo.Price;
                        totalMedPrice += (quantity * price);

                        ListViewItem item = new ListViewItem(stt.ToString());
                        item.SubItems.Add(medicine);
                        item.SubItems.Add(unit);
                        item.SubItems.Add(quantity.ToString());
                        item.SubItems.Add(price.ToString());
                        item.Tag = viMed;

                        lvMed.Items.Add(item);
                    }
                }
                txtMedicineFee.Text = totalMedPrice.ToString();
            }
            else
            {
                lvMed.Items.Clear();
            }
        }

        public void clearTXT()
        {
            txtPatientID.Clear();
            txtMedicalFee.Clear();
            txtMedicineFee.Clear();
            txtTotal.Clear();
            txtViDate.Clear();
            cbFullname.SelectedIndex = -1;
            lvMed.Items.Clear();
        }

        private void FormMedBill_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            FormManager.mainForm.Show();
            clearTXT();
            FormManager.medBillForm.Hide();
        }

        private void FormMedBill_Activated(object sender, EventArgs e)
        {
            clearTXT();
        }
    }
}
